<script setup>
import { ref } from 'vue'
import { useAuthStore } from '../stores/modules/auth'
import { ElDialog } from 'element-plus'
import { Message, Iphone, QuestionFilled, ChatDotRound } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const authStore = useAuthStore()
const router = useRouter()
const dialogVisible = ref(false)

const userProfile = ref({
  username: authStore.user?.username || '',
  email: authStore.user?.email || '',
  phone: '未绑定手机'
})

const showProfile = () => {
  dialogVisible.value = true
}

const goToPath = (path) => {
  router.push(path)
  dialogVisible.value = false
}

</script>

<template>
  <div class="profile-trigger" @click="showProfile">
    <img
      :src="'https://api.dicebear.com/7.x/bottts/svg?seed=' + userProfile.username"
      alt="avatar"
      class="avatar"
    />
    <span class="username">{{ userProfile.username }}</span>
  </div>

  <el-dialog
    v-model="dialogVisible"
    title="个人信息"
    width="400px"
    :show-close="true"
    :lock-scroll="false"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    class="profile-dialog"
    draggable
  >
    <div class="profile-info">
      <div class="avatar-section">
        <img
          :src="'https://api.dicebear.com/7.x/bottts/svg?seed=' + userProfile.username"
          alt="avatar"
          class="large-avatar"
        />
        <div class="user-name">{{ userProfile.username }}</div>
      </div>

      <div class="info-section">
        <div class="info-item">
          <el-icon><Message /></el-icon>
          <span class="info-label">邮箱</span>
          <span class="info-value">{{ userProfile.email }}</span>
        </div>

        <div class="info-item">
          <el-icon><Iphone /></el-icon>
          <span class="info-label">手机</span>
          <span class="info-value">暂不支持手机注册</span>
        </div>
      </div>

      <div class="action-section">
        <div class="action-item" @click="goToPath('/help')">
          <el-icon><QuestionFilled /></el-icon>
          <span>帮助文档</span>
        </div>
        <div class="action-item" @click="goToPath('/feedback')">
          <el-icon><ChatDotRound /></el-icon>
          <span>建议反馈</span>
        </div>
      </div>

      <div class="logout-section">
        <el-button type="danger" class="logout-button" @click="authStore.logout"
          >退出登录</el-button
        >
      </div>
    </div>
  </el-dialog>
</template>

<style scoped>
.profile-trigger {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px;
  border-radius: 4px; transition: background-color 0.3s;
}

.profile-trigger:hover {
  background-color: var(--menu-hover-bg);
}
.profile-trigger:hover .username {
  color: var(--menu-text-hover-color);
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
}

.username {
  color: var(--color-text);
}

:deep(.profile-dialog) {
  border-radius: 8px;
  background-color: var(--el-bg-color-overlay);
}

:deep(.el-dialog__header) {
  margin: 0;
  padding: 20px;
  border-bottom: 1px solid var(--el-border-color-lighter);
  background-color: var(--el-bg-color-overlay);
}

:deep(.el-dialog__body) {
  padding: 0;
  background-color: var(--el-bg-color-overlay);
}

:deep(.el-dialog__title) {
  color: var(--color-text);
}

.profile-info {
  padding: 20px;
  background-color: var(--el-bg-color);
}

.avatar-section {
  text-align: center;
  margin-bottom: 24px;
}

.large-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-bottom: 12px;
}

.user-name {
  font-size: 18px;
  font-weight: 500;
  color: var(--color-text);
}

.info-section {
  margin-bottom: 24px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--el-border-color-lighter);
}

.info-item .el-icon {
  margin-right: 12px;
  font-size: 18px;
  color: var(--color-text-soft);
}

.info-label {
  color: var(--color-text-soft);
  width: 60px;
}

.info-value {
  flex: 1;
  color: var(--color-text);
}

.action-section {
  display: flex;
  justify-content: space-around;
  margin-bottom: 24px;
  padding: 12px 0;
  border-bottom: 1px solid var(--el-border-color-lighter);
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  padding: 5px 16px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.action-item:hover {
  background-color: var(--menu-hover-bg);
  color: var(--menu-text-hover-color);
}

.action-item .el-icon {
  font-size: 24px;
  margin-bottom: 8px;
  color: var(--color-text);
}

.action-item span {
  font-size: 14px;
  color: var(--color-text);
}
.action-item:hover span {
  color: var(--menu-text-hover-color);
}

.logout-section {
  text-align: center;
}

.logout-button {
  width: 100%;
}
:deep(.el-popup-parent--hidden) {
  width: 100% !important;
}
</style>
